package com.lxl.xm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxl.xm.mapper.StudentTaskMapper;
import com.lxl.xm.model.vo.TaskCompleteVO;
import com.lxl.xm.service.TaskBranchService;
import com.lxl.xm.service.TaskService;
import com.lxl.xm.common.ErrorCode;
import com.lxl.xm.exception.BusinessException;
import com.lxl.xm.model.entity.StudentTask;
import com.lxl.xm.model.entity.Task;
import com.lxl.xm.service.StudentTaskService;
import com.lxl.xm.mapper.TaskMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 86135
* @description 针对表【task(任务表)】的数据库操作Service实现
* @createDate 2024-03-09 12:50:04
*/
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task>
    implements TaskService {

    @Resource
    TaskMapper taskMapper;

    @Resource
    StudentTaskService studentTaskService;

    @Resource
    StudentTaskMapper studentTaskMapper;

    @Resource
    TaskBranchService taskBranchService;

    /**
     * 添加任务参数校验
     * @param task
     * @param isAdd
     */
    @Override
    public void validTask(Task task, boolean isAdd) {
        if (task == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"任务对象为空");
        }
    }

    @Override
    public List<Task> getOngoingTasks() {
        return taskMapper.selectOngoingTasks();
    }

    /**
     * 学生参加主线任务
     * 设计两个表的操作
     * @param studentTask
     * @return
     */
    @Override
    @Transactional
    public boolean joinTask(StudentTask studentTask) {
        //学生任务表插入数据
        Long taskId = studentTask.getTaskId();
        //是否参加过
        LambdaQueryWrapper<StudentTask> studentTaskLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentTaskLambdaQueryWrapper.eq(StudentTask::getStudentId,studentTask.getStudentId())
                .eq(StudentTask::getTaskId,studentTask.getTaskId());
        StudentTask selectOne = studentTaskMapper.selectOne(studentTaskLambdaQueryWrapper);
        if (selectOne != null){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"学生已参加过该任务");
        }
        //任务表更新
        //update task set join_number = join_number + 1
        LambdaUpdateWrapper<Task> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Task::getId,taskId)
                .setSql("join_number = join_number + 1");
        return studentTaskService.save(studentTask) && this.update(updateWrapper);
    }

    @Override
    public Long totalTasks() {
        long count = this.count();
        long count1 = taskBranchService.count();
        return count1 + count;
    }

    /**
     * 近一天完成的任务
     * @return
     */
    @Override
    public List<TaskCompleteVO> finishedInOneDay() {
        //主线

        return null;
        //支线
    }


}




